# -*- coding: utf-8 -*-
import os
from www.shortcuts import *


class Static(StaticHandler):
    rule = Rule('/static/<path:path>')
    static_dir = os.path.join(base_path, 'static')


class Login(Handler):
    rule = Rule('/login/')
    def get(self):
        tmpl = env.get_template('login.html')
        return Response(tmpl.render(
            request = self.request,
            errors = self.errors,
        ))

    def post(self):
        username = self.request.form.get('username', u'').strip()
        password = self.request.form.get('password', u'').strip()
        user = self.store.find(User, User.username==username).one()
        if user:
            if user.checkPassword(password):
                user.login(self.request)
                return redirect('/')
            else:
                self.errors['login'] = u'用户名或密码错误'
        else:
            self.errors['login'] = u'用户名或密码错误'
        return self.get()

class Logout(Handler):
    rule = Rule('/logout/')
    def get(self):
        logout(self.request)
        return redirect('/')
